<template>
  <div class="import-file">
    <el-steps :active="step" finish-status="success">
      <el-step title="上传文件"></el-step>
      <el-step title="执行导入"></el-step>
      <el-step title="导入完成"></el-step>
    </el-steps>
    <el-row class="flex" v-show="step===0">
      <div class="file-left">
        <div class="img">
          <fa-icon class="file-left-icon fa-cloud-download"></fa-icon>
        </div>
      </div>
      <div class="file-right">
        <div class="title">填写导入的字典数据</div>
        <div class="content">请按照数据模板的格式准备导入数据，模板中的表头名称不可更改，表头行不能删除，单次导入的数据不超过1000条。</div>
        <div class="content">
          <a class="btn" @click="downloadTemplete">下载模板</a>
        </div>
      </div>
    </el-row>
    <el-row class="flex upload" v-show="step===0">
      <div class="file-left">
        <div class="img">
          <fa-icon class="file-left-icon fa-cloud-upload"></fa-icon>
        </div>
      </div>
      <div class="file-right">
        <div class="title">上传填好的字典数据表</div>
        <div class="content">文件后缀名必须为xls 或xlsx （即Excel格式），文件大小不得大于10M</div>
        <!-- <div class="content"><a class="btn">上传文件</a></div> -->
        <div class="content">
          <el-upload class="upload-demo" ref="upload" action="" :on-change="handlePreview" :on-remove="handleRemove" :file-list="fileList" :auto-upload="false">
            <a class="btn" slot="trigger">上传文件</a>
          </el-upload>
        </div>
      </div>
    </el-row>
    <el-row v-if="step===0" class="execution-btn" style="margin-top: 80px">
      <el-button type="primary" :disabled="file===''" @click="submitExcel">下一步</el-button>
    </el-row>
    <el-row v-show="step===1">
      <el-row class="execution">
        <el-row class="flex">
          <div class="execution-left">
            <div class="img">
              <fa-icon class="execution-icon fa-check-circle-o"></fa-icon>
            </div>
          </div>
          <div class="execution-right">
            <div class="title">正常数量条数</div>
            <div class="content">{{ uploadResult.success }}条</div>
          </div>
        </el-row>
        <el-row class="flex fail">
          <div class="execution-left">
            <div class="img">
              <fa-icon class="execution-icon fa-question-circle"></fa-icon>
            </div>
          </div>
          <div class="execution-right">
            <div class="title">异常数量条数</div>
            <div class="content">{{ uploadResult.fail }}条</div>
          </div>
        </el-row>
      </el-row>
      <el-row class="execution-btn">
        <el-button type="primary" @click="submitExcel">下一步</el-button>
        <el-button @click="step=0">重新上传</el-button>
      </el-row>
      <el-row class="execution-tip">
        <el-row class="execution-tip-title">
          <el-col>异常提示：</el-col>
        </el-row>
        <el-row class="execution-tip-content">
          <el-col v-for="(item, index) in uploadResult.errorList" :key="index" :span="24">{{ item }}</el-col>
        </el-row>
      </el-row>
    </el-row>
    <el-row v-show="step===2">
      <el-col :span="18" :offset="3">
        <div style="margin: 200px 0 30px;">
          <el-progress :percentage="percentage" :stroke-width="15"></el-progress>
        </div>
        <div style="font-size: 18px;color: #999;text-align: center">数据正在执行导入中，请勿关闭或刷新页面</div>
      </el-col>
    </el-row>
    <el-row v-show="step===3">
      <el-row class="complete">
        <div class="img">
          <fa-icon class="execution-icon fa-check-circle-o"></fa-icon>
        </div>
        <div class="title">批量导入完成</div>
        <div class="content">成功导入员工数量{{ uploadResult.success }}条</div>
        <el-row class="complete-btn">
          <el-button type="primary" style="width:100px" @click="fishData">完成</el-button>
        </el-row>
      </el-row>
    </el-row>
  </div>
</template>

<script>
import { exportExcel } from '@/utils/util'
import { CONSTANT_OBJ } from '@/utils/constant'
import api from '../api'

export default {
  name: 'import-file',
  data () {
    return {
      step: 0,
      fileList: [],
      file: '',
      uploadResult: {
        success: 0,
        fail: 0,
        errorList: []
      },
      percentage: 0,
      loading: false
    }
  },
  methods: {
    submitUpload () {
      this.$refs.upload.submit()
      this.step = 1
    },
    handleRemove (file, fileList) {
      this.file = ''
      this.fileList = []
    },
    downloadTemplete () {
      let parmas = {}
      let path = `/v1/api/esale-pay/repay/overdue/template/excel`
      let name = '逾期数据导入模板'
      console.log('模板下载')
      exportExcel(path, parmas, name)
    },
    // 校验excel是否符合规格
    handlePreview (file, fileList) {
      console.log(file)
      const isXlsorXlsx = file.raw.type === 'application/vnd.ms-excel' || file.raw.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      const isLt5M = file.raw.size / 1024 / 1024 < 10
      if (!isXlsorXlsx) {
        this.$message.error('上传文件支持.xls和.xlsx格式')
      }
      if (!isLt5M) {
        this.$message.error('上传文件大小不能超过10MB!')
      }
      if (isXlsorXlsx && isLt5M) {
        if (fileList.length > 1) {
          this.fileList = [fileList[fileList.length - 1]]
        } else {
          this.fileList = [...fileList]
        }
        this.file = file
      }
      return isXlsorXlsx && isLt5M
    },
    // 提交excel
    submitExcel () {
      const { file, step } = this
      let timer
      if (step === 1) {
        this.step = 2
        timer = setInterval(() => {
          if (this.percentage <= 90) {
            this.percentage += 10
          }
        }, 100)
      }
      const param = new FormData()
      this.loading = true
      param.append('file', file.raw, file.name)
      param.append('add', step === 1)
      api.postImport(param).then(res => {
        this.loading = false
        if (step === 1) {
          clearInterval(timer)
        }
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          console.log(data)
          if (step === 0) {
            this.uploadResult = { ...this.uploadResult, ...data }
            this.step = 1
          }
          if (step === 1) {
            this.percentage = 100
            this.step = 3
          }
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 完成
    fishData () {
      this.$emit('updateData')
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../../assets/css/common.less";
.import-file {
  .file-left {
    display: flex;
    width: 100px;
    height: 120px;
    background: rgba(245, 245, 245, 1);
    .img {
      margin: auto;
    }
    .file-left-icon {
      font-size: 67px;
      color: #c0c4cc;
    }
  }
  .file-right {
    padding-left: 20px;
    .title {
      margin: 10px 0;
      font-size: 16px;
      font-weight: bold;
    }
    .content {
      margin: 10px 0;
    }
    .btn {
      margin-right: 20px;
      color: @theme-color;
      cursor: pointer;
    }
  }
  .upload {
    margin-top: 20px;
  }
  .execution {
    border: 1px solid #ddd;
    margin-top: 20px;
    padding: 10px;
    .execution-icon {
      font-size: 48px;
      background: #fff;
      color: @theme-success-color;
    }
    .execution-left {
      display: flex;
      .img {
        margin: auto;
      }
    }
    .execution-right {
      padding-left: 20px;
      .title {
        margin: 10px 0;
        font-weight: bold;
      }
      .content {
        margin: 10px 0;
        color: @theme-color;
      }
    }
    .fail {
      margin-top: 20px;
    }
  }
  .execution-btn {
    text-align: center;
    margin-top: 20px;
  }
  .execution-tip {
    height: 300px;
    margin-top: 20px;
    padding: 10px;
    border: 1px solid #ddd;
    background: #f1f1f1;
    color: #ff6a00;
    overflow: hidden;
    overflow-y: auto;
    .execution-tip-title {
      font-weight: bold;
    }
    .execution-tip-content {
      padding: 5px 0;
    }
  }
  .complete {
    text-align: center;
    margin-top: 50px;
    .img {
      font-size: 70px;
      color: @theme-success-color;
    }
    .title {
      margin: 10px 0;
      font-size: 24px;
      font-weight: bold;
    }
    .complete-btn {
      margin-top: 50px;
    }
  }
}
</style>

<style lang="less">
@import "../../../assets/css/common.less";
.import-file {
  .el-step__head.is-process {
    border-color: @theme-color;
    .el-step__icon.is-text {
      background: @theme-color;
      color: #fff;
    }
  }
  .el-step__head.is-success {
    border-color: @theme-color;
    .el-step__icon-inner.is-status {
      color: @theme-color;
    }
  }
  .el-step__title.is-success {
    color: @theme-color;
  }
  .fa-icon {
    margin-right: 0;
  }
}
</style>
